package com.warm.pump.module.work.service.impl;

import com.warm.pump.executor.InstanceService;
import com.warm.pump.module.work.bean.po.gen.Work;
import com.warm.pump.module.work.dao.gen.WorkDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.warm.pump.module.work.dao.gen.WorkSubjectDao;
import com.warm.pump.module.work.bean.po.gen.WorkSubject;
import com.warm.pump.module.work.service.IWorkSubjectService;
import com.warm.pump.frame.gen.bean.PageBean;
import org.springframework.transaction.annotation.Transactional;


@Service("WorkSubjectService")
public class WorkSubjectServiceImpl implements IWorkSubjectService {
    @Autowired
    private WorkSubjectDao workSubjectDao;
    @Autowired
    private InstanceService instanceService;
    @Autowired
    private WorkDao workDao;

    @Override
    public WorkSubject queryObject(Long id) {
        return workSubjectDao.selectByPrimaryKey(id);
    }

    public WorkSubject queryObject(Map<String, Object> map) {
        return workSubjectDao.selectByMap(map);
    }

    @Override
    public List<WorkSubject> queryList(Map<String, Object> map) {
        return workSubjectDao.getListByMap(map);
    }

    @Override
    public PageBean getPageBeanByMap(Map map, int pageNo, int pageSize, Long jobId) {
        if (null != jobId) {
//			Map<String,Object> skdMap = new HashMap<>();
//			skdMap.put("jobId",jobId);
//			List<Long> longs = skdBoDao.selectWorkIdByJobId(skdMap);
//			if(null != longs && longs.size()>0){
//				map.put("id_notIn",longs);
//			}

        }
        return workSubjectDao.getPageBeanByMap(map, pageNo, pageSize);
    }


    @Override
    public int queryTotal(Map<String, Object> map) {
        return workSubjectDao.countTotalByMap(map);
    }

    @Override
    public void save(WorkSubject workSubject) {
        workSubjectDao.insertSelective(workSubject);
    }

    @Override
    public void update(WorkSubject workSubject) {
        workSubjectDao.updateByPrimaryKeySelective(workSubject);
    }

    @Override
    public void delete(Long id) {
        workSubjectDao.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(Map<String, Object> map) {
        workSubjectDao.deleteByMap(map);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("id_in", ids);
        workSubjectDao.deleteByMap(map);
    }

    @Override
    public void run(Long[] ids) {
        for (Long id : ids) {
            createInstByWorkSuject(id);
        }
    }

    /**
     * 通过专题创建实例
     *
     * @param subjectId
     */
    @Transactional
    protected void createInstByWorkSuject(Long subjectId) {
        WorkSubject subject = workSubjectDao.selectByPrimaryKey(subjectId);
        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("subjectId", subject.getId());
        itemMap.put("status", 1);
        itemMap.put("orderBy", "order_num");
        List<Work> workList = workDao.getListByMap(itemMap);
        if (null != workList && workList.size() > 0) {
            //创建调度实例
            String instId = instanceService.createInst("workSubject", subjectId, subject.getSubjectName(), workList.size(), "专题列表中立即执行");
            for (Work work : workList) {
                //创建调度实例明细
                instanceService.createInstDtl(instId, work.getId(), work.getWorkName(), null, null, null);
            }
        }
    }
}
